package loader
{
	//因为这个不是用回调函数的方法,所以必须先添加事件侦听,再调用loadFile方法
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import events.MyEvent;
	public class MuilteFilesLoader extends EventDispatcher
	{
		private var myLoader:Loader;
		private var urls:Vector.<String > ;
		private var bitmaps:Array;
		private var callBack:Function;
		private var count:uint = 0;
		private var mode:int=0;//0 一次加载完后再发出事件 //1 加载好一个就发出一次事件
		public function MuilteFilesLoader(s:Vector.<String>,_mode:int=0,target:IEventDispatcher=null)
		{
			super(target);
			urls=s;
			mode=_mode;
			bitmaps=new Array();
		}
		
		private function handleIOERROR(e:IOErrorEvent):void
		{
			trace("handleIOERROR");
			this.dispatchEvent(new MyEvent("fileNotFound",urls[count]));//发出未找到文件的事件
			loadNext();
			
		}
		
		public function loadFile():void
		{
			removeLoader();
			var path:String=urls[count];
			if(isFlv(path))
			{
				if(mode==0)
				{
					bitmaps.push({content:path,index:count,type:"flv"});
					loadNext();
					return;
					
				}
				else
				{
					this.dispatchEvent(new MyEvent("complete",{content:path,index:count,type:"flv"}));
					loadNext();
					return;
				}
			}
			else if(isMP(path))
			{
				if(mode==0)
				{
					bitmaps.push({content:path,index:count,type:"mp"});
					loadNext();
					return;
				}
				else
				{
					this.dispatchEvent(new MyEvent("complete",{content:path,index:count,type:"mp"}));
					loadNext();
					return;
				}
			}
			else
			{
				myLoader = new Loader();
				
				myLoader.load(new URLRequest(path));
				myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
				myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleIOERROR);
			}
			
		}
		
		private function loadNext():void
		{
			count++;
			if (count < urls.length)
			{
				
				loadFile();
			}
			else 
			{
				if(mode==0)
				{
					this.dispatchEvent(new MyEvent("complete",bitmaps));
				}
			}
			
		}
		
		
		private function onComplete(e:Event):void
		{
			//trace(e.currentTarget.loader.contentLoaderInfo.contentType);
			//判断加载的是图片还是swf
			if (e.currentTarget.loader.contentLoaderInfo.contentType == "image/jpeg" || e.currentTarget.loader.contentLoaderInfo.contentType=="image/png")
			{
				var bm:Bitmap = e.target.content as Bitmap;
				bm.smoothing = true;
				if(mode==0)
				{
					bitmaps.push({content:bm,index:count,type:"bmp"});
				}
				else
				{
					this.dispatchEvent(new MyEvent("complete",{content:bm,index:count,type:"bmp"}));
				}
			}
			else if(e.currentTarget.loader.contentLoaderInfo.contentType == "application/x-shockwave-flash")
			{
				var _swf:MovieClip = e.target.content as MovieClip;
				if(mode==0)
				{
					bitmaps.push({content:_swf,index:count,type:"swf"});
				}
				else
				{
					this.dispatchEvent(new MyEvent("complete",{content:_swf,index:count,type:"swf"}));
				}
				
			}
			
			
			e.target.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			e.target.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleIOERROR);
			removeLoader();
			loadNext();
		}
		
		private function isFlv(path:String):Boolean
		{
			var index:uint=path.lastIndexOf(".");
			var type:String=path.substr(index+1);
			type.toLowerCase();
			if(type=="flv"||type=="f4v"||type=="mp4"||type=="m4a"||type=="mp4v"||type=="3gp"||type=="3g2"||type=="mov")
			{
				return true;
			}
			return false;
		}
		
		private function isMP(path:String):Boolean
		{
			var index:uint=path.lastIndexOf(".");
			var type:String=path.substr(index+1);
			type.toLowerCase();
			if(type=="mp3")
			{
				return true;
			}
			return false;
		}
		
		
		private function removeLoader():void
		{
			if (myLoader)
			{
				if(myLoader.contentLoaderInfo.hasEventListener(Event.COMPLETE))
				{
					myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
					myLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleIOERROR);
				}
				
				myLoader = null;
			}
		}//end removeXmlLoader
	}
}